<?php
namespace app\admin\controller;

use think\Controller;
use think\Env;
use think\response\View;
use think\Validate;
[folder start]
use app\model\{tblName};
[folder end]	

class {subFolder}Controller extends Base
{
[folder start]
    //region   [tblTitle]模快
	//use app\model\{tblName};
    //列表查询
    public function [tblName]list()
    {
        if(request()->isPost()){
            $post = request()->post();
            $pagesize=request()->post("pagesize");
            $pageindex=request()->post("pageindex");

            //查询条件
            $cond=[];
				[loop start]
            $[fldName]=request()->post("[fldName]");
            if(strlen($[fldName])>0){
                $cond["[fldName]"]=["=",$[fldName]];
            }
				[loop end]^
            //print_r($cond);die; 

			$[tblName]=new {tblName}();
            //分页总数
            $total=$[tblName]->where($cond)->count();
            $data["total"]=$total;

            //数据列表
            $rows=$[tblName]->where($cond)
				->field("{strNames}")
				->page($pageindex,$pagesize)
				->order("id desc")->select();
            $data["rows"]=$rows;
            $this->success($data);
        }
        return view();
    }

    //获取
    public function [tblName]view()
    {
        if(request()->isPost()) {
            $[tblName]=new {tblName}();
            $id = request()->post("id");
            if ($id == 0) {
                $data["id"]=0;
                $this->success($data);
            }else {
                $model=$[tblName]->find($id);
                if ($model) {
                    $this->success($model);
                } else {
                    $this->error("记录获取失败");
                }
            }
        }
        return view();
    }
 
    //添加
    public function [tblName]edit()
    {
        if (request()->isPost()) {
            /* {strNames} */
            $id=request()->post("id");
            $post = request()->post();

            // 验证规则
            $validate = new Validate([
  [loop start]
                "[fldName]"  => "require",
  [loop end]^

			],[
  [loop start]
                "[fldName].require"=>"[fldTitle]不能为空",
  [loop end]^

            ]);

            $[tblName]=new {tblName}();
            if($id) {
                //修改保存
                if(!$validate->check($post)){
                    $this->validateError($validate->getError());
                }
                $[tblName]=$[tblName]->find($id);

                //post 数据处理
                //print_r($post);die;
                $[tblName]->save($post);
                $this->success($[tblName]);
            }else{
                //添加保存
                if(!$validate->check($post)){
                    $this->validateError($validate->getError());
                }

                //post 数据处理
  [loop start]
				//$post["[fldName]"]=[fldDefaultValue];
  [loop end]^
				//$post["addDate"]=date("Y-m-d H:i:s");
                //print_r($post);die;
                $rows= $[tblName]->save($post);

                if($rows>0){
                    $this->success($[tblName]);
                }else{
                    $this->error("添加失败");
                }
            }
        }
        return view();
    }

    //删除
    public function  [tblName]delete()
    {
        $id=request()->post("id");
        if(!$id){
            $this->error("非法的参数");
        }
        $[tblName]=new {tblName}();
        $result=$[tblName]->find($id)->delete();
        if($result === false){
            $this->error("删除失败");
        }else{
            $this->success($id);
        }
	}
    //endregion
[folder end]	
}
